﻿using System;
using System.Linq;
using System.Web.Mvc;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Enums;
using MiniWeb.Task;
using MiniWeb.Web.Controllers.Admin.ProductsClassManager.ViewModels;
using WFramework.Extensions;
using WFramework.Mappers;
using WFramework.MvcPager;
using WFramework;
using System.Collections.Generic;
namespace MiniWeb.Web.Controllers.Admin.ProductsClassManager
{

    public class ProductsClassManagerController : BaseController
    {
        private readonly ProductsClassTask _productsclassTask = new ProductsClassTask();
        private readonly ProductsTypeTask _productsTypeTask = new ProductsTypeTask();

        public ActionResult List(int? tid, int pageIndex = 1, int pageSize = 20)
        {
            var pagedList = _productsclassTask.GetPagedList(tid,pageIndex, pageSize);

            ViewData["typelist"] = _productsTypeTask.GetAll(100, "", null).ToSelectListForAll(c => c.ID.ToString(), c => c.TypeName);
            var model = new PagedList<ProductsClass>(pagedList.ToList(), pageIndex, pageSize, pagedList.TotalItemCount);

            return View(model);
        }


        public ActionResult Save(int? id)
        {

            ViewData["typelist"] = _productsTypeTask.GetAll(100, "", null).ToSelectListForAll(c => c.ID.ToString(), c => c.TypeName);
            var model = new SaveProductsClass();
            model.Sort = 1;
            if (id != null)
            {
                var item = _productsclassTask.GetById((int)id);
                if (item == null)
                    return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                model = EntityMapper.Map<ProductsClass, SaveProductsClass>(item);
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Save(SaveProductsClass savemodel)
        {
            ViewData["typelist"] = _productsTypeTask.GetAll(100, "", null).ToSelectListForAll(c => c.ID.ToString(), c => c.TypeName);
            if (ModelState.IsValid)
            {

                if (savemodel.ID == null)
                {
                    var model = new ProductsClass
                                    {
                                        ClassName = savemodel.ClassName,
                                        ProductsTypeID = savemodel.ProductsTypeID,
                                        CreateDate = DateTime.Now,
                                        Sort = savemodel.Sort
                                    };
                    _productsclassTask.Add(model);
                }
                else
                {
                    var model = _productsclassTask.GetById((int)savemodel.ID);

                    if (model == null)
                        return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                    model.ClassName = savemodel.ClassName;
                    model.ProductsTypeID = savemodel.ProductsTypeID;
                    model.Sort = savemodel.Sort;
                    _productsclassTask.Update(model);
                }
                return AlertMsg("保存成功", HttpContext.Request.UrlReferrer.PathAndQuery);

            }

            return View(savemodel);
        }


        public ActionResult GetClass(int tid)
        {
           
            var list = _productsclassTask.GetAll(null, "ProductsTypeID = " + tid.ToString(),null).Select(c => new SelectListItem
            {
                Text = c.ClassName,
                Value = c.ID.ToString(),
            }).ToList();
            list.Insert(0, new SelectListItem
            {
                Selected = true,
                Text = "请选择",
                Value = "",
            });
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        #region 删除用户信息 Delete

        [HttpPost]
        public void Delete(int id)
        {
            _productsclassTask.Delete(id);
        }

        #endregion
    }
}